package com.dcy.znoa.service.estate.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.estate.model.advice.SelectEstateAdviceModel;
import com.dcy.znoa.dao.estate.EstateAdviceMapper;
import com.dcy.znoa.dao.publiz.CompanyDao;
import com.dcy.znoa.domain.estate.EstateAdvice;
import com.dcy.znoa.domain.publiz.Company;
import com.dcy.znoa.service.estate.EstateAdviceService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 物业建议 服务实现类
 * </p>
 *
 * @author cook
 * @since 2018-05-21
 */
@Service
public class EstateAdviceServiceImpl extends ServiceImpl<EstateAdviceMapper, EstateAdvice> implements EstateAdviceService {

    @Autowired
    EstateAdviceMapper eaMapper;

    @Autowired
    CompanyDao companyDao;

    @Override
    public JsonForList selectList(SelectEstateAdviceModel model) throws InvocationTargetException, IllegalAccessException {
        EstateAdvice cp = new EstateAdvice();
        BeanUtils.copyProperties(cp, model);
        Page page = new Page(model.getPageNum(), model.getPageSize());
        List<EstateAdvice> list = eaMapper.selectListMapper(model, page);
        return new JsonForList(list, page.getTotal());
    }

    @Override
    public boolean add(EstateAdvice cp) throws BusinessException {
        if(cp==null){return false;}
        Long compId = cp.getCompId();
        Company company = companyDao.selectById(compId);
        if(company==null){
            throw new BusinessException("404","此员工对应公司不存在");
        }
        Long esId = company.getEsId();
        if(esId==null){
            throw new BusinessException("404","此员工对应公司不存在绑定的物业公司");
        }
        cp.setEsId(esId);
        return insert(cp);
    }
}
